package com.tarena.security.admin.controller;

import com.tarena.security.admin.assemble.AppAssemble;
import com.tarena.security.admin.entity.db.App;
import com.tarena.security.admin.entity.param.AppParam;
import com.tarena.security.admin.entity.query.AppQuery;
import com.tarena.security.admin.result.JsonResult;
import com.tarena.security.admin.service.IAppService;
import com.tarena.security.admin.vo.AppVO;
import com.tedu.inn.protocol.validation.group.Create;
import com.tedu.inn.protocol.validation.group.Update;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * app 前端控制器
 * </p>
 *
 * @author Ly
 * @since 2023-04-10
 */
@RestController
@Api(tags = "App")
@RequestMapping("/app")
public class AppController {
    @Resource
    private IAppService appService;

    @Resource
    private AppAssemble appAssemble;

    @PostMapping("/add-new")
    @ApiOperation("新增App")
    public JsonResult<String> addNewApp(@Validated(Create.class) AppParam appParam){
        appService.addNewApp(appParam);
        return JsonResult.ok("新增成功");
    }
    @ApiOperation("获取App列表")
    @GetMapping("/getAppList")
    public JsonResult<List<AppVO>> getAppList(AppQuery appQuery){
        List<App> list=appService.getAppList(appQuery);
        List<AppVO> vos = appAssemble.assembleDtosToVos(list);
        return JsonResult.ok(vos);
    }

    @ApiOperation("修改App")
    @PostMapping("/update")
    public JsonResult<String> updateApp(@Validated(Update.class) AppParam appParam){
        appService.updateByID(appParam);
        return JsonResult.ok("修改成功");
    }

    @ApiOperation("根据ID获取App")
    @GetMapping("/getById/{id}")
    public JsonResult<AppVO> getAppById(@PathVariable Integer id){
        App app=appService.getAppById(id);
        AppVO vo = appAssemble.assembleDtosToVo(app);
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据ID删除APP")
    @PostMapping("/deleteById")
    public JsonResult<String> deleteAppById(Integer ... id){
        appService.deleteAppById(id);
        return JsonResult.ok("删除成功");
    }
}
